package com.sc.VenusQueryBeta.ui.fragments.hotel.child.tabs;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseRecycleFragment;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.api.Host;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.hotel.tabs.KaiXuanContract;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.bean.home.VipListBean;
import com.sc.VenusQueryBeta.presenter.hotel.tabs.KaiXuanPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 凯旋门Fragment
 */

public class KaiXuanFragment extends BaseRecycleFragment<KaiXuanContract.KaiXuanPresenter> implements KaiXuanContract.IKaiXuanView, OnItemClickListener {
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.zhanfang_phone)
    ImageView zhanfangPhone;
    @BindView(R.id.changmian_phone)
    ImageView changmianPhone;
    Unbinder unbinder;
    @BindView(R.id.tv_zf_phone)
    TextView tvZfPhone;
    @BindView(R.id.tv_cm_phone)
    TextView tvCmPhone;
    Unbinder unbinder1;
    @BindView(R.id.iv_top_image)
    ImageView ivTopImage;
    Unbinder unbinder2;
    @BindView(R.id.ll_aomen)
    LinearLayout llAomen;

    Unbinder unbinder3;
    private String toPhone;
    private AlertView mCallView;
    private VipListBean.DataBean.ClubListBean listBean;

    /**
     * 实例化对象
     *
     * @return
     */
    public static KaiXuanFragment newInstance() {
        Bundle args = new Bundle();
        KaiXuanFragment fragment = new KaiXuanFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return KaiXuanPresenter.newInstance();
    }

    @Override
    public void updateContentList(List<Object> list) {

    }

    @Override
    public void itemNotifyChanged(int position) {

    }


    @Override
    public void showNetworkError() {

    }

    @Override
    public void showLoadMoreError() {

    }

    @Override
    public void showNoMoreData() {

    }

    @Override
    protected void onErrorViewClick(View view) {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_kaixuan;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {

            Glide.with(MyApplication.app).load(Host.HostImg + listBean.getClubImageUrl()).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.img_banner_1).into(ivTopImage);
            address.setText(listBean.getClubAddress() == null ? "" : listBean.getClubAddress());
            tvCmPhone.setText(listBean.getGroundTel() == null ? "" : listBean.getGroundTel());
            tvZfPhone.setText(listBean.getAccountantTel() == null ? "" : listBean.getAccountantTel());


    }


    @OnClick({R.id.zhanfang_phone, R.id.changmian_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zhanfang_phone://帐房查数
                if (listBean != null) {
                    call(listBean.getAccountantTel());
                } else {
                    ToastUtils.showToast("数据错误");
                }
                break;
            case R.id.changmian_phone://场面电话
                if (listBean != null) {
                    call(listBean.getGroundTel());
                } else {
                    ToastUtils.showToast("数据错误");
                }
                break;
        }
    }

    /**
     * 打电话
     *
     * @param phone 号码
     */
    private void call(String phone) {
        toPhone = phone;
        mCallView = new AlertView("拨打电话", phone, "取消", new String[]{"呼叫"}, null, mActivity, AlertView.Style.Alert, this).setCancelable(true);
        mCallView.show();
        //监听消失
        mCallView.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(Object o) {

            }
        });
    }

    @Override
    public void onItemClick(Object o, int position) {
//        closeKeyboard();
        if (position != AlertView.CANCELPOSITION) {//呼叫按钮
            //用intent启动拨打电话
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + toPhone));
            startActivity(intent);
        }
    }

    @Override
    public void getBundle(Bundle bundle) {
        if (bundle != null) {
            listBean = (VipListBean.DataBean.ClubListBean) bundle.getSerializable(Constants.VIP_CLUB_LIST);
//            ToastUtils.showToast(listBean.getClubName());
        } else {
//            ToastUtils.showToast("null");
        }
    }


}
